#include "Turtoze.h"
#include "Fire.h"
#include "Objects/Particle.h"
#include <iostream>

using namespace std;

Turtoze::Turtoze(GameState *gameState) : PhysicalObject(gameState){
	m_texture = gameState->getTextureManager().getTexture("turtoze.png");

	addProperty("Turtoze");
	addProperty("enemy");
	setLayer(3);

	setSize(b2Vec2(6.0, 6.0f * (float(m_texture->getSize().y)/m_texture->getSize().x)));
	setRotation(0.0f);

	getBodyDef()->type = b2_dynamicBody;
	getBodyDef()->fixedRotation = true;
	b2FixtureDef *fixDef = new b2FixtureDef;
	fixDef->filter.groupIndex = -1;
	b2PolygonShape *shape = new b2PolygonShape;
	shape->SetAsBox(m_size.x/6.0f,m_size.y/4.0f);
	fixDef->shape = shape;
	fixDef->density = 2.0f;
	fixDef->restitution = 0.0f;
	addFixtureDef(fixDef);


	fixDef = new b2FixtureDef;
	shape = new b2PolygonShape;
	shape->SetAsBox(m_size.x/16.0f,m_size.y/16.0f, b2Vec2(-m_size.x/4.0f,-m_size.y/4.0f), 0);
	fixDef->shape = shape;
	fixDef->density = 2.0f;
	fixDef->restitution = 0.0f;
	fixDef->isSensor = true;
	addFixtureDef(fixDef);

	fixDef = new b2FixtureDef;
	shape = new b2PolygonShape;
	shape->SetAsBox(m_size.x/16.0f,m_size.y/16.0f, b2Vec2(m_size.x/4.0f,-m_size.y/4.0f), 0);
	fixDef->shape = shape;
	fixDef->density = 2.0f;
	fixDef->restitution = 0.0f;
	fixDef->isSensor = true;
	addFixtureDef(fixDef);

	m_state = 0;
	m_time = 0;
	m_particleTimer = 0;

	
}


void Turtoze::firstUpdate(){

}


void Turtoze::update(double dt){
	m_particleTimer += dt;
	m_time += dt;

	float vel = 4;
	b2Vec2 linVel = m_body->GetLinearVelocity();
	if(m_state==0){
		m_body->SetLinearVelocity(b2Vec2(-vel,linVel.y));
		if(!m_botLeft){
			m_state=1;
		}
	}else if(m_state==1){
		m_body->SetLinearVelocity(b2Vec2(vel,linVel.y));
		if(!m_botRight){
			m_state=0;
		}
	}
	
	
	
	if(m_particleTimer > 0.05f){
		m_particleTimer = 0;
		for(int i = -2; i < 3; i++){
			b2Vec2 particlePos = b2Vec2(getPosition().x+i, getPosition().y-1);
			particlePos += b2Vec2((((rand()%10)/10.0f)-0.5f)*0.5f, (((rand()%10)/10.0f)-0.5f)*0.5f);
			Particle *particle = m_gameState->getCurrentScreen()->createParticle();
			particle->setShader(m_gameState->getShaderManager().getShader("iceParticle"));
			particle->setColor(0.3f, 0.8f, 1.0f, 0.7f);
			particle->setEndColor(0.8f, 0.8f, 0.8f, 0.5f);
			particle->setSize(b2Vec2(5.0f, 5.0f));
			particle->setPosition(particlePos);
			particle->setLifeTime(1.0f);
		}
	}

	if(!isAlive()){
		for(int i = -4; i < 6; i++){
			for(int j = -1; j < 3; j++){
				b2Vec2 particlePos = b2Vec2(getPosition().x+i*0.1f, getPosition().y+j);
				b2Vec2 posSum = b2Vec2((((rand()%10)/10.0f)-0.5f)*0.5f, (((rand()%10)/10.0f)-0.5f)*0.5f);
				posSum *= 4.0f;
				particlePos += posSum;
				
				Particle *particle = m_gameState->getCurrentScreen()->createParticle();
				particle->setShader(m_gameState->getShaderManager().getShader("particle"));
				if(rand()%2){
					particle->setColor(1.0f, 0.4f, 0.15f, 0.6f);
				}else{
					particle->setColor(1.0f, 0.2f, 0.15f, 0.4f);
				}

				particle->setEndColor(0.2f, 0.2f, 0.2f, 0.0f);
				particle->setSize(b2Vec2(6.0f, 8.0f));
				particle->setPosition(particlePos);
				particle->setLifeTime(0.5f+rand()%6/20);
				particle->setVel(b2Vec2((((rand()%10)/10.0f)-0.5f)*4, 8-rand()%10/2.0));
				//, rand()%2+2);
			}
		}

		for(int i = -2; i < 3; i++){
			for(int j = -1; j < 2; j++){
				b2Vec2 particlePos = b2Vec2(getPosition().x+i, getPosition().y+j);
				b2Vec2 posSum = b2Vec2((((rand()%10)/10.0f)-0.5f)*0.5f, (((rand()%10)/10.0f)-0.5f)*0.5f);
				posSum *= 4.0f;
				particlePos += posSum;
				
				Particle *particle = m_gameState->getCurrentScreen()->createParticle();
				particle->setShader(m_gameState->getShaderManager().getShader("iceParticle"));
				if(rand()%2){
					particle->setColor(0.25f, 0.2f, 0.2f, 0.7f);
				}else{
					particle->setColor(0.4f, 0.3f, 0.3f, 0.7f);
				}
				particle->setEndColor(0.0f, 0.0f, 0.0f, 0.5f);
				particle->setSize(b2Vec2(10.0f, 10.0f));
				particle->setPosition(particlePos);
				particle->setLifeTime(1.5f);
				particle->setVel(b2Vec2((((rand()%10)/10.0f)-0.5f)*4, 5));
				//, rand()%2+2.0f);
			}
		}

		m_gameState->getSoundManager().playSound("enemyFire.wav");
	}
	
}


void Turtoze::draw(){

	b2Vec2 pos = getPosition();

	//drawFixtures();

	glColor3f(1.0f, 1.0f, 1.0f);
	glPushMatrix();
	{
		glTranslatef(pos.x, pos.y, 0); 
		sf::Texture::bind(m_texture);
		float mirror = 1;
		if(m_state==1){
			mirror = -1;
		}
		glScalef(getSize().x*mirror, getSize().y, 1);
		glBegin(GL_QUADS);
		{
			glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, -0.2f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.2f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, -0.2f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, -0.2f);
		}
		glEnd();

		sf::Texture::bind(NULL);

		
	}
	glPopMatrix();


	
}

void Turtoze::beginContact(b2Contact *contact){
	b2Fixture *myFixture;
	PhysicalObject *me;
	PhysicalObject *other;

	getContactEntities(contact, &me, &other, &myFixture);

	if(other->hasProperty("Fire")){
		Fire *fire = static_cast<Fire*>(other);
		if(fire->getState()==2){
			this->kill();
		}
	}

	if(other->hasProperty("ground")){
		int fixturePos=0;
		for(b2Fixture *f = m_body->GetFixtureList(); f; f = f->GetNext()){
			if(f==myFixture){
				break;
			}else{
				fixturePos++;
			}
		}

		if(fixturePos == 1){
			m_botLeft = true;
		}else if(fixturePos == 0){
			m_botRight = true;
		}
	}
}

void Turtoze::endContact(b2Contact *contact){
	b2Fixture *myFixture;
	PhysicalObject *me;
	PhysicalObject *other;
	getContactEntities(contact, &me, &other, &myFixture);

	if(other->hasProperty("Fire")){
		Fire *fire = static_cast<Fire*>(other);
		if(fire->getState()==2){
			this->kill();
		}
	}

	if(other->hasProperty("ground")){
		int fixturePos=0;
		for(b2Fixture *f = m_body->GetFixtureList(); f; f = f->GetNext()){
			if(f==myFixture){
				break;
			}else{
				fixturePos++;
			}
		}
		if(fixturePos == 1){
			m_botLeft = false;
		}else if(fixturePos == 0){
			m_botRight = false;
		}
	}

}


void Turtoze::PreSolve(b2Contact* contact, const b2Manifold* oldManifold){
	PhysicalObject *me = (PhysicalObject*)contact->GetFixtureA()->GetBody()->GetUserData();
	PhysicalObject *other = (PhysicalObject*)contact->GetFixtureB()->GetBody()->GetUserData();

	if(me != this){ // objectB is the thing you've contacted
		PhysicalObject *temp;
		temp = me;
		me = other;
		other = temp;
	}

	if(other->hasProperty("OnewayPlatform")){
		if(me->getPosition().y-m_size.y/8 < other->getPosition().y){
			contact->SetEnabled(false);
		}
	}
}